<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>机会管理</span>
      </div>
      <div class="searchAdd">
        <el-button class="addBtn" type="primary" @click="ShowAddDialog">
          添加
        </el-button>
        <el-col :span="15" style="width: 400px">
          <el-select
            v-model="Page.name"
            placeholder="请选择员工"
            @change="searchByName()"
          >
            <el-option
              v-for="item in allUser"
              :key="item.empLoginid"
              :label="item.empName"
              :value="item.empName"
            >
            </el-option>
          </el-select>
          <el-button
            type="info"
            style="margin-left: 20px"
            @click="searchAllInfo()"
          >
            查询所有数据
          </el-button>
        </el-col>
      </div>
      <div class="InfoTable">
        <el-table
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          max-height="400px"
          stripe
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form
                label-position="left"
                inline
                class="demo-table-expand"
                style="padding-left: 100px"
              >
                <el-form-item label="机会名称：">
                  <span>{{ props.row.chanceName }}</span>
                </el-form-item>
                <el-form-item label="机会编码：">
                  <span>{{ props.row.chanceCode }}</span>
                </el-form-item>
                <el-form-item label="机会类型：">
                  <span>{{ props.row.chanceType }}</span>
                </el-form-item>
                <el-form-item label="录入人：">
                  <span>{{ props.row.entryName }}</span>
                </el-form-item>
                <el-form-item label="录入时间：">
                  <span>{{ props.row.entryTime }}</span>
                </el-form-item>
                <el-form-item label="被分配人：">
                  <span>{{ props.row.assignerName }}</span>
                </el-form-item>
                <el-form-item label="备注：">
                  <span>{{ props.row.des }}</span>
                </el-form-item>
                <el-form-item label="机会内容：" style="width: 60% !important">
                  <span>{{ props.row.content }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column fixed prop="chanceId" label="ID" width="100">
          </el-table-column>
          <el-table-column prop="chanceName" label="机会名称" width="150">
          </el-table-column>
          <el-table-column prop="chanceType" label="机会类型" width="150">
          </el-table-column>
          <el-table-column prop="entryTime" label="录入时间" width="180">
          </el-table-column>
          <el-table-column prop="entryName" label="录入人" width="150">
          </el-table-column>
          <el-table-column prop="des" label="备注">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="160">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="changeRow(scope.row)"
                type="text"
                size="small"
              >
                修改
              </el-button>
              <el-button
                @click.native.prevent="AllotRow(scope.row)"
                type="text"
                size="small"
              >
                分配
              </el-button>
              <el-button
                @click.native.prevent="deleteRow(scope.row)"
                type="text"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :page-size="Page.size"
          :pager-count="7"
          :current-page="Page.current"
          layout="prev, pager, next"
          :page-count="Page.pageCount"
          @current-change="handleCurrentChange"
          ref="pageCurrent"
        >
        </el-pagination>
      </div>
      <!-- 添加对话框 -->
      <el-dialog
        title="添加"
        :visible.sync="AddDialogFormVisible"
        width="30%"
        @close="addDialogClosed"
      >
        <el-form
          ref="addDialogRef"
          label-width="65px"
          :model="addItem"
          :rules="addRules"
          style="padding-right: 50px"
        >
          <el-form-item label="机会编码" label-width="120px" prop="chanceCode">
            <el-input v-model="addItem.chanceCode"></el-input>
          </el-form-item>
          <el-form-item label="机会名称" label-width="120px" prop="chanceName">
            <el-input v-model="addItem.chanceName"></el-input>
          </el-form-item>
          <el-form-item label="机会类型" label-width="120px" prop="chanceType">
            <el-select v-model="addItem.chanceType" placeholder="请选择">
              <el-option
                v-for="item in chanceTypeList"
                :key="item.chanceId"
                :label="item.chanceName"
                :value="item.chanceName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机会内容" label-width="120px" prop="content">
            <el-input
              type="textarea"
              :rows="4"
              v-model="addItem.content"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" label-width="120px" prop="des">
            <el-input v-model="addItem.des"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addChanceData">添加</el-button>
          <el-button @click="AddDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog
        title="修改机会信息"
        :visible.sync="ChangeDialogFormVisible"
        width="30%"
      >
        <el-form :model="itemDate" style="padding-right: 50px">
          <el-form-item label="机会编码" label-width="120px" prop="chanceCode">
            <el-input v-model="itemDate.chanceCode"></el-input>
          </el-form-item>
          <el-form-item label="机会名称" label-width="120px" prop="chanceName">
            <el-input v-model="itemDate.chanceName"></el-input>
          </el-form-item>
          <el-form-item label="机会类型" label-width="120px" prop="chanceType">
            <el-select v-model="itemDate.chanceType" placeholder="请选择">
              <el-option
                v-for="item in chanceTypeList"
                :key="item.chanceId"
                :label="item.chanceName"
                :value="item.chanceName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机会内容" label-width="120px" prop="content">
            <el-input
              type="textarea"
              :rows="4"
              v-model="itemDate.content"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" label-width="120px" prop="des">
            <el-input v-model="itemDate.des"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="changeSubmit">修改</el-button>
          <el-button @click="ChangeDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 分配对话框 -->
      <el-dialog
        title="分配"
        :visible.sync="AllotDialogFormVisible"
        width="30%"
        @close="AllotDialogClosed"
      >
        <el-form
          ref="AllotDialogRef"
          label-width="65px"
          :model="AllotItem"
          :rules="AllotRules"
          style="padding: 0 20px; height: 400px; overflow-y: auto"
        >
          <el-form-item label="机会编码" label-width="120px" prop="chanceCode">
            <el-input v-model="AllotItem.chanceCode"></el-input>
          </el-form-item>
          <el-form-item label="机会名称" label-width="120px" prop="chanceName">
            <el-input v-model="AllotItem.chanceName"></el-input>
          </el-form-item>
          <el-form-item label="机会类型" label-width="120px" prop="chanceType">
            <el-select v-model="AllotItem.chanceType" placeholder="请选择">
              <el-option
                v-for="item in chanceTypeList"
                :key="item.chanceId"
                :label="item.chanceName"
                :value="item.chanceName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机会内容" label-width="120px" prop="content">
            <el-input
              type="textarea"
              :rows="4"
              v-model="AllotItem.content"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" label-width="120px" prop="des">
            <el-input v-model="AllotItem.des"></el-input>
          </el-form-item>
          <el-form-item label="机会来源" label-width="120px" prop="origin">
            <el-input v-model="AllotItem.origin"></el-input>
          </el-form-item>
          <el-form-item label="录入人" label-width="120px" prop="entryName">
            <el-select v-model="AllotItem.entryName" placeholder="请选择">
              <el-option
                v-for="item in allUser"
                :key="item.empLoginid"
                :label="item.empName"
                :value="item.empName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="录入时间" label-width="120px" prop="entryTime">
            <el-input v-model="AllotItem.entryTime" :disabled="true"></el-input>
          </el-form-item>
          <hr />
          <br />
          <el-form-item label="分配" label-width="120px" prop="assignerName">
            <el-select v-model="AllotItem.assignerName" placeholder="请选择">
              <el-option
                v-for="item in allUser"
                :key="item.empLoginid"
                :label="item.empName"
                :value="item.empName"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="AllotData">分配</el-button>
          <el-button @click="AllotDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import pubsub from "pubsub-js";

export default {
  name: "chance",
  data() {
    return {
      // 加载状态
      loading: "",
      // 当前登录员工
      loginUserName: "",
      // 添加窗口控制器
      AddDialogFormVisible: false,
      // 修改窗口控制器
      ChangeDialogFormVisible: false,
      // 分配窗口控制器
      AllotDialogFormVisible: false,
      // 页面
      Page: {
        name: "",
        current: 1,
        size: 6,
        pageCount: 1,
      },
      chanceTypeList: [
        { chanceId: 1, chanceName: "官网流量" },
        { chanceId: 2, chanceName: "转介绍" },
        { chanceId: 3, chanceName: "媒体进线" },
        { chanceId: 4, chanceName: "客户来访" },
      ],
      // 单条修改数据
      itemDate: {},
      // 单条分配数据
      AllotItem: {},
      // 添加机会
      addItem: {
        chanceCode: "",
        chanceName: "",
        chanceType: "",
        content: "",
        des: "",
      },
      // 表格数据
      tableData: [],
      // 所有员工
      allUser: [],
      // 添加数据规则
      addRules: {
        chanceCode: [
          { required: true, message: "请输入机会编码", trigger: "blur" },
        ],
        chanceName: [
          { required: true, message: "请输入机会名称", trigger: "blur" },
        ],
        chanceType: [
          {
            required: true,
            message: "请选择机会类型",
            trigger: ["blur", "change"],
          },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
        des: [{ required: true, message: "请输入备注信息", trigger: "blur" }],
      },
      // 分配数据规则
      AllotRules: {
        chanceCode: [
          { required: true, message: "请输入机会编码", trigger: "blur" },
        ],
        chanceName: [
          { required: true, message: "请输入机会名称", trigger: "blur" },
        ],
        chanceType: [
          {
            required: true,
            message: "请选择机会类型",
            trigger: ["blur", "change"],
          },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
        des: [{ required: true, message: "请输入备注信息", trigger: "blur" }],
        origin: [
          { required: true, message: "请输入机会来源", trigger: "blur" },
        ],
        entryName: [
          {
            required: true,
            message: "请选择录入人",
            trigger: ["blur", "change"],
          },
        ],
        assignerName: [
          {
            required: true,
            message: "请选择分配人",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  mounted() {
    this.getAllUserInfo();
    this.getAllChanceInfo();
    pubsub.subscribe("loginUser", (_, data) => {
      this.loginUserName = data;
    });
  },
  methods: {
    // 通过员工姓名查找
    searchByName() {
      this.getAllChanceInfo();
    },
    // 获取所有机会表格信息
    async getAllChanceInfo() {
      this.loading = true;
      if (this.Page.name == "") {
        const { data: res } = await this.$http.post("/chancemanage/getall", {
          current: this.Page.current,
          size: this.Page.size,
        });
        if (res.status != 200) {
          this.loading = false;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      } else {
        const { data: res } = await this.$http.post(
          "/chancemanage/getAllByEmpName",
          {
            current: this.Page.current,
            size: this.Page.size,
            empName: this.Page.name,
          }
        );
        if (res.status != 200) {
          this.loading = false;
          this.Page.name = "";
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      }
    },
    // 页面改变
    handleCurrentChange(currentPage) {
      this.Page.current = currentPage;
      this.getAllChanceInfo();
    },
    // 获取所有员工信息
    async getAllUserInfo() {
      const { data: res } = await this.$http.post("/user/getAllUserInfo");
      if (res.status != 200)
        return this.$message.error("系统错误，请重新登录！");
      this.allUser = res.data;
    },
    // 按钮查询所有信息
    searchAllInfo() {
      this.Page.name = "";
      this.getAllChanceInfo();
    },
    // 添加对话框
    ShowAddDialog() {
      this.AddDialogFormVisible = true;
    },
    //监听添加窗口关闭事件
    addDialogClosed() {
      this.resetAddForm();
      this.getAllChanceInfo();
    },
    // 重置添加
    resetAddForm() {
      this.$refs.addDialogRef.resetFields();
    },
    //监听分配窗口关闭事件
    AllotDialogClosed() {
      this.resetAllotForm();
      this.getAllChanceInfo();
    },
    // 重置分配
    resetAllotForm() {
      this.$refs.AllotDialogRef.resetFields();
    },
    // 修改对话框
    changeRow(row) {
      this.itemDate = row;
      this.ChangeDialogFormVisible = true;
    },
    // 分配对话框
    AllotRow(row) {
      this.AllotItem = row;
      if (this.AllotItem.entryTime == null) {
        this.AllotItem.entryTime = new Date().toLocaleString();
      }
      this.AllotDialogFormVisible = true;
    },
    // 删除
    deleteRow(row) {
      this.$confirm("是否删除该计划?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.post(
            "/chancemanage/deletebyid",
            {
              id: row.chanceId,
            }
          );
          if (res.status != 200)
            return this.$notify.error({
              title: "失败",
              message: res.msg,
              duration: 2000,
            });
          this.getAllChanceInfo();
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 2000,
          });
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
    // 修改
    async changeSubmit() {
      const { data: res } = await this.$http.post(
        "/chancemanage/updateonbyid",
        this.itemDate
      );
      if (res.status != 200) {
        this.loading = false;
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.ChangeDialogFormVisible = false;
      this.getPrefData();
    },
    // 添加机会
    async addChanceData() {
      this.$refs.addDialogRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/chancemanage/insertone",
          this.addItem
        );
        if (res.status != 200) {
          return this.$message.error(res.msg);
        }
        this.$message.success("添加成功");
        this.AddDialogFormVisible = false;
        this.getAllChanceInfo();
      });
    },
    // 分配
    async AllotData() {
      this.$refs.AllotDialogRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/chancemanage/distribute",
          {
            chanceId: this.AllotItem.chanceId, //id
            assignerName: this.AllotItem.assignerName, // 分配给
            entryName: this.AllotItem.entryName, //录入人
            entryTime: this.AllotItem.entryTime, //录入时间
            operatorName: this.loginUserName, // 操作人
            currentName: this.loginUserName, //当前操作人
            origin: this.AllotItem.origin,
          }
        );
        if (res.status != 200) {
          return this.$message.error(res.msg);
        }
        this.$message.success("添加成功");
        this.AllotDialogFormVisible = false;
        this.getAllChanceInfo();
      });
    },
  },
};
</script>

<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-content: center;
  padding: 0 50px;
}
.searchAdd {
  width: 93%;
  margin: 0 auto;
  height: 40px;
  margin-bottom: 20px;
}

.InfoTable {
  width: 93%;
  margin: 0 auto;
}
.addBtn {
  float: left;
  width: 100px;
  height: 40px;
  margin-right: 20px;
}
</style>
